package org.iteam.xlz.core.service.membershipcard;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.recharge.Recharges;

public class ChargeService extends BaseService<Recharges>{
	private static ChargeService instance;
	
	private ChargeService(){}

	public static ChargeService getInstance(){
		if(null==instance){
			instance=new ChargeService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from recharges where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<Recharges> query(long page,long rows){
		try {
			return super.query("select * from recharges where 1=1 order by createtime desc limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Recharges>();
		}
	}
	
	public boolean create(Recharges model){
		SqlBuilder sql=SqlBuilder.createInsert("recharges");
		sql.addField("rid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("rechargename",model.getRechargename());
		sql.addField("amount",model.getAmount());
		sql.addField("donationamount",model.getDonationamount());
		sql.addField("status",model.getStatus());
		sql.addField("createtime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(Recharges model){
		SqlBuilder sql=SqlBuilder.createUpdate("recharges");
		sql.addField("rechargename",model.getRechargename());
		sql.addField("amount",model.getAmount());
		sql.addField("donationamount",model.getDonationamount());
		sql.addField("status",model.getStatus());
		sql.addWhere("rid",model.getRid());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public Recharges findOne(String rid){
		try {
			return super.show("select * from recharges where rid=?",rid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new Recharges();
		}
	}
}
